package com.tinyideatech.admin.controller.mall;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tinyideatech.common.core.controller.BaseController;
import com.tinyideatech.common.core.entity.ApiResult;
import com.tinyideatech.common.core.entity.PageResult;
import com.tinyideatech.common.core.text.Convert;
import com.tinyideatech.common.utils.StringUtils;
import com.tinyideatech.system.entity.MallClassify;
import com.tinyideatech.system.service.IMallClassifyService;
import com.tinyideatech.system.service.IMallGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * @author zhangy-5
 * @date 2019-08-26
 */

@Controller
@RequestMapping("/mall/classify")
public class MallClassifyController extends BaseController {

    @Autowired
    private IMallClassifyService classifyService;

    @Autowired
    private IMallGoodsService mallGoodsService;

    @GetMapping()
    public String list() {
        return "mall/classify/list";
    }

    @GetMapping("/add")
    public String add() {
        return "mall/classify/add";
    }

    @GetMapping("/edit/{id}")
    public String mallClassifyEdit(
            @PathVariable("id") Long id,
            ModelMap map
    ) {
        MallClassify classify = classifyService.getById(id);
        map.addAttribute("classify", classify);
        return "mall/classify/edit";
    }

    @ResponseBody
    @PostMapping("/list")
    public PageResult list(MallClassify mallClassify) {
        QueryWrapper<MallClassify> qw = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(mallClassify.getClassifyName())) {
            qw.like("classify_name", mallClassify.getClassifyName());
        }
        IPage<MallClassify> page = startPage();
        IPage<MallClassify> classifyList = classifyService.page(page, qw);
        return page(classifyList);
    }

    @ResponseBody
    @PostMapping("/save")
    public ApiResult save(
            MallClassify classify
    ) {
        classifyService.saveOrUpdate(classify);
        return success();
    }

    @ResponseBody
    @PostMapping("/remove")
    public ApiResult remove(
            String ids
    ) {
        Long[] classifyIds = Convert.toLongArray(ids);
        classifyService.removeByIds(Arrays.asList(classifyIds));
        return success();
    }

    @PostMapping("/changeStatus")
    @ResponseBody
    public ApiResult changeStatus(MallClassify mallClassify) {
        classifyService.changeStatus(mallClassify);
        return success();
    }

    @GetMapping("/model/count")
    @ResponseBody
    public ApiResult count(@RequestParam long id) {
        return ApiResult.success(mallGoodsService.countModel(id));
    }
}
